package finalgame.GUI;

import org.lwjgl.input.Keyboard;
import org.newdawn.slick.opengl.Texture;

import finalgame.Game;
import finalgame.Engine.Battle;
import finalgame.Engine.GeneratePokemon;
import finalgame.Engine.pokemon.Pokemon;
import finalgame.Engine.trainer.HeroTrainer;
import finalgame.Engine.trainer.Trainer;
import finalgame.GUI.Character.Direction;
import finalgame.GUI.battle.BattleScreen;
import finalgame.GUI.battle.BattleScreenManager;
import finalgame.Graphics.Animation;
import finalgame.Graphics.Location;
import finalgame.Graphics.Render2D;
import finalgame.Graphics.RenderEngine;

public class Hero extends Character {
	public Hero(String name) {
		super(name);
		solid = true;
		setTrainer(new HeroTrainer());
	}

	public void setLocation(Location loc) {
		Location backup = vLoc;
		super.setLocation(loc);
		if(stage.collisionDetect(this)) {
			super.setLocation(backup);
		}
		stage.setLoc(vLoc.x(), vLoc.y());
	}
	
	public void render() {
		Location renderLoc = new Location(0, 0);
		if(stage != null) {
			renderLoc = stage.getPRenderLoc(vLoc);
			if(renderLoc == null) return;
		}
		Animation anim = RenderEngine.getAnimation(characterName + "." + direction.getName());
		anim.draw((int) renderLoc.x() - 5 * anim.getWidth() / 12,
				(int) renderLoc.y() - 10 * anim.getHeight() / 12);
	}
	
	public void update() {
		moved = false;
		if(Keyboard.isKeyDown(Keyboard.KEY_DOWN) && !isKeyDown) {
			setDirection(Direction.DOWN);
			isKeyDown = true;
		} else if(Keyboard.isKeyDown(Keyboard.KEY_UP) && !isKeyDown) {
			setDirection(Direction.UP);
			isKeyDown = true;
		} else if(Keyboard.isKeyDown(Keyboard.KEY_LEFT) && !isKeyDown) {
			setDirection(Direction.LEFT);
			isKeyDown = true;
		} else if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && !isKeyDown) { 
			setDirection(Direction.RIGHT);
			isKeyDown = true;
		} else {
			isKeyDown = false;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_A)) {
			Character c = stage.interact(this);
			if(c != null) {
				enterBattle(c);
			}
		}
		
		if(isKeyDown) {
			translate(direction.getDelta());
			RenderEngine.getAnimation(characterName + "." + direction.getName()).setLocation(vLoc);
			RenderEngine.getAnimation(characterName + "." + direction.getName()).update();
		}
	}
	
	private void enterBattle(Character c) {
		Trainer t = c.getTrainer();
		String text = "Trainer " + t.getName() + " wants to battle!\n" +
				t.getName() + " sent out " + t.getCurrentPokemon().getName() + ".";
		Game.say(text, "Alert");
		Battle battle = new Battle(Game.game.hero.getTrainer(), t);
		BattleScreen scene = (BattleScreen) BattleScreenManager.getNewBattleScene("battlegrass");
		scene.use(battle);
		Game.game.delve(scene);
	}
}
